package txc.test.web.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mycat on 2017/4/17.
 */
public class ResultData {
    // protected Logger logger = LoggerFactory.getLogger(getClass());
	/*
	 * 调用结果
	 */
    private boolean success;

    /*
     * 错误或成功代码
     */
    private int code;

    /*
     * 服务器端返回消息
     */
    private String msg;

    private String language;

    /*
     * 数据对象
     */
    private Object obj;

    private Map<String, Object> map;

    /*
     * 集合数据对象
     */
    private List list;

    public ResultData() {
    }

    public boolean success() {
        return success;
    }

    public int getCode() {
        return code;
    }

    public ResultData setCode(int code) {
        this.code = code;
        return this;
    }

    public boolean isSuccess() {
        return success;
    }

    public ResultData setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    /**
     * 分解code返回提示信息 未设置code时，如果有msg信息直接返回msg，否则默认返回500错误并打印日志提示开发者
     */
    public String getMsg() {
        if (code <= 0) {
            if (msg != null && !"".equals(msg)) {
                return msg;
            }
            code = 500;
//			logger.warn("Msg Code is Invalid !");
        }
        language = "zh_CN";
        return success ? "success" : "error";
    }

    public ResultData setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public Object getObj() {
        return obj;
    }

    public ResultData setObj(Object data) {
        this.obj = data;
        return this;
    }

    public List getList() {
        return list;
    }

    public ResultData setList(List list) {
        this.list = list;
        return this;
    }

    public Map getMap() {
        return map;
    }

    public ResultData setMap(Map<String, Object> map) {
        this.map = map;
        return this;
    }

    public ResultData addAttribute(String key, Object value) {
        if (this.map != null) {
            this.map.put(key, value);
        } else {
            this.map = new HashMap<String, Object>();
            this.map.put(key, value);
        }
        return this;
    }

    // public ResultData setPage(Page page){
    // this.list = page.getList();
    // if(this.map == null){
    // this.map = new HashMap<String,Object>();
    // }
    // this.map.put("pageNo", page.getPageNo());
    // this.map.put("pageSize", page.getPageSize());
    // this.map.put("count", page.getCount());;
    // return this;
    // }

    public static ResultData result(boolean result) {
        return new ResultData().setSuccess(result).setCode(result ? 200 : 500);
    }
}
